package com.miqtech.master.wyrecorder.ui;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.miqtech.master.wyrecorder.R;
import com.miqtech.master.wyrecorder.annotations.LayoutId;
import com.miqtech.master.wyrecorder.annotations.Title;
import com.miqtech.master.wyrecorder.base.BaseAppCompatActivity;
import com.miqtech.master.wyrecorder.common.SimpleTextWatcher;
import com.miqtech.master.wyrecorder.utils.PreferenceUtils;
import com.miqtech.master.wyrecorder.utils.imageloader.AsyncImage;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by arvin on 2016/10/28.
 */
@LayoutId(R.layout.activity_recordersetting)
@Title(title = "直播设置")
public class RecorderSettingActivity extends BaseAppCompatActivity implements RadioGroup.OnCheckedChangeListener {
    @BindView(R.id.et_live_title)
    EditText etLiveTitle;
    @BindView(R.id.rb_orientation_horizontal)
    RadioButton rbOrientationHorizontal;
    @BindView(R.id.rb_orientation_vertical)
    RadioButton rbOrientationVertical;
    @BindView(R.id.rg_orientation)
    RadioGroup rgOrientation;
    @BindView(R.id.rb_quality_m)
    RadioButton rbQualityM;
    @BindView(R.id.rb_quality_h)
    RadioButton rbQualityH;
    @BindView(R.id.rb_quality_xh)
    RadioButton rbQualityXh;
    @BindView(R.id.rg_quality)
    RadioGroup rgQuality;
    @BindView(R.id.btn_start)
    Button btnStart;
    @BindView(R.id.img_clear)
    ImageView imgClear;
    @BindView(R.id.ll_root)
    LinearLayout llRoot;
    @BindView(R.id.ll_start)
    LinearLayout llStart;
    @BindView(R.id.img_start)
    ImageView imgStart;
    @BindView(R.id.ivGamePic)
    ImageView ivGamePic;
    @BindView(R.id.tvGameName)
    TextView tvGameName;
    @BindView(R.id.rlLiveTitleInput)
    RelativeLayout rlLiveTitleInput;
    private int mOrientation = 1;
    private int quality = 1; //1 超清 2 高清 3 标清
    private int mVideoWidth = 960;
    private int mVideoHeight = 544;
    private String imageUrl;
    private String gameName;
    private String gameID;
    private String gameIcon;
    public static final String LIVE_TITLE = "liveTitle";
    public static final String SREEN_ORIENTATION = "sreenOrientation";
    public static final String QUALITY = "quality";
    private Animation animiationShaking;


    @Override
    public void initViews(Bundle savedInstanceState) {
        initView();
    }


    public void initView() {
        animiationShaking = AnimationUtils.loadAnimation(this, R.anim.shaking);
        Bundle bundle = this.getIntent().getExtras();
        if (bundle != null) {
            imageUrl = bundle.getString("imageUrl");
            gameName = bundle.getString("gameName");
            gameID = bundle.getString("gameID");
            gameIcon = bundle.getString("gameIcon");
        }
        if (!TextUtils.isEmpty(imageUrl)) {
            AsyncImage.displayImage(imageUrl, ivGamePic, R.drawable.default_img);
        }
        if (!TextUtils.isEmpty(gameName)) {
            tvGameName.setText(gameName);
        }
        String liveTitle = PreferenceUtils.getString(LIVE_TITLE);
        if (!TextUtils.isEmpty(liveTitle)) {
            etLiveTitle.setText(liveTitle);
        } else {
            etLiveTitle.setHint("请输入直播标题");
        }
        int screenOriention = PreferenceUtils.getInt(SREEN_ORIENTATION);
        if (screenOriention == 0) {
            rgOrientation.check(R.id.rb_orientation_horizontal);
            setOrientation(R.id.rb_orientation_horizontal);
        } else {
            rgOrientation.check(R.id.rb_orientation_vertical);
            setOrientation(R.id.rb_orientation_vertical);
        }
        int quality = PreferenceUtils.getInt(QUALITY);
        if (quality == 1) {
            rgQuality.check(R.id.rb_quality_xh);
            setQuality(R.id.rb_quality_xh);
        } else if (quality == 2) {
            rgQuality.check(R.id.rb_quality_h);
            setQuality(R.id.rb_quality_h);
        } else if (quality == 3) {
            rgQuality.check(R.id.rb_quality_m);
            setQuality(R.id.rb_quality_m);
        }else{
            rgQuality.check(R.id.rb_quality_h);
            setQuality(R.id.rb_quality_h);
        }
        rgOrientation.setOnCheckedChangeListener(this);
        rgQuality.setOnCheckedChangeListener(this);
        etLiveTitle.addTextChangedListener(new SimpleTextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                if (etLiveTitle.getText().length() > 0) {
                    imgClear.setVisibility(View.VISIBLE);
                } else {
                    imgClear.setVisibility(View.GONE);
                }
            }
        });


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    /**
     * 设置质量参数
     */
    private void setQuality(int checkId) {
        if (checkId == R.id.rb_quality_xh) {
            mVideoWidth = 720;
            mVideoHeight = 1280;
            quality = 1;
        } else if (checkId == R.id.rb_quality_h) {
            mVideoWidth = 544;
            mVideoHeight = 960;
            quality = 2;
        } else if (checkId == R.id.rb_quality_m) {
            mVideoWidth = 480;
            mVideoHeight = 848;
            quality = 3;
        }
    }

    /**
     * 设置方向参数
     *
     * @param checkId
     */
    private void setOrientation(int checkId) {
        if (checkId == R.id.rb_orientation_horizontal) {
            mOrientation = 0;
        } else {
            mOrientation = 1;
        }
    }

    /**
     *
     */
    private void setSizeWithOrientation() {
        int tmpBig = Math.max(mVideoWidth, mVideoHeight);
        int tmpSmall = Math.min(mVideoWidth, mVideoHeight);
        if (mOrientation == 0) {
            mVideoWidth = tmpSmall;
            mVideoHeight = tmpBig;
        } else {
            mVideoWidth = tmpBig;
            mVideoHeight = tmpSmall;
        }
    }

    @OnClick({R.id.ibLeft, R.id.ll_start, R.id.img_clear, R.id.ivGamePic})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ibLeft:
                onBackPressed();
                break;
            case R.id.ll_start:
                setSizeWithOrientation();
                if (TextUtils.isEmpty(etLiveTitle.getText().toString().trim())) {
                    showToast("直播标题和内容不能为空");
                    rlLiveTitleInput.startAnimation(animiationShaking);

                } else {
                    Bundle data = new Bundle();
                    data.putString("gameId", gameID);
                    data.putString("liveTitle", etLiveTitle.getText().toString());
                    data.putInt("videoHeight", mVideoHeight);
                    data.putInt("videoWidth", mVideoWidth);
                    data.putInt("screen", mOrientation);
                    data.putString("gameIcon", gameIcon);
                    PreferenceUtils.putString(LIVE_TITLE, etLiveTitle.getText().toString());
                    PreferenceUtils.putInt(SREEN_ORIENTATION, mOrientation);
                    PreferenceUtils.putInt(QUALITY, quality);
                    jumpToActivity(ScreenRecorderActivity.class, data);
                    finish();
                }
                break;
            case R.id.img_clear:
                etLiveTitle.setText("");
                break;
            case R.id.ivGamePic:
                finish();
                break;
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (group.getId()) {
            case R.id.rg_orientation:
                setOrientation(checkedId);
                break;
            case R.id.rg_quality:
                setQuality(checkedId);
                break;
        }
    }
}
